export const getSetting = () => {
  return new Promise(
    (resolve, reject) => {
      wx.getSetting({
        success: (result) => {
          resolve(result);
        },
        fail: (err) => {
          reject(err);
        }
      })
    }
  ).catch((e)=>{});
}

export const chooseAddress = () => {
  return new Promise(
    (resolve, reject) => {
      wx.chooseAddress({
        success: (result) => {
          resolve(result);
        },
        fail: (err) => {
          reject(err);
        }
      })
    }
  ).catch((e)=>{});
}

export const openSetting = () => {
  return new Promise(
    (resolve, reject) => {
      wx.openSetting({
        success: (result) => {
          resolve(result);
        },
        fail: (err) => {
          reject(err);
        }
      })
    }
  ).catch((e)=>{});
}


export const showModal = ({ content }) => {
  return new Promise(
    (resolve, reject) => {
      wx.showModal({
        title: '提示',
        content: content,
        success: (res) => {
          resolve(res);
        },
        fail: (err) => {
          reject(err);
        }
      })
    }
  ).catch((e)=>{});
}

export const showToast = ({ title, content }) => {
  return new Promise(
    (resolve, reject) => {
      wx.showToast({
        title: title,
        icon: 'none',
        success: (res) => {
          resolve(res);
        },
        fail: (err) => {
          reject(err);
        }
      })
    }
  ).catch((e)=>{});
}

export const updateToken = () => {
  return new Promise(
    (resolve, reject) => {
      wx.login({
        timeout: 5000,
        success: (res) => {
          const { code } = res;
          wx.request({
            url: 'http://localhost:50364/api/my/user/wxlogin',
            method: "POST",
            data: { code: code },
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: (res) => {
              resolve(res);
            },
            fail: (err) => { reject(err) }
          })
        }
      })
    }
  ).catch((e)=>{});
}

// Promise形式的getUserProfile
export const getUserProfile = () => {
  return new Promise(
    (resolve, reject) => {
      wx.getUserProfile({
        desc: '用户登录',
        lang: 'zh_CN',
        success: (result) => { resolve(result); },
        fail: (err) => { reject(err); }
      })
    }
  ).catch((e)=>{});
}



// promise形式的微信支付函数
export const requestPayment = (pay) => {
  return new Promise(
    (resolve, reject) => {
      wx.requestPayment({
        ...pay,
        success: (res) => {
          resolve(res);
        },
        fail: (err) => {
          reject(err);
        }
      })
    }
  ).catch((e)=>{});
}
